package com.itheima.innerclass3;

import java.util.Arrays;
import java.util.Comparator;

public class Test {

    public static void main(String[] args) {

        //完成数组的排序，理解其中匿名内部类的用法
        Student[] students = new Student[5];
        students[0] = new Student("张三", 18, "男", 1.81);
        students[1] = new Student("李四", 14, "女", 1.72);
        students[2] = new Student("王五", 20, "男", 1.83);
        students[3] = new Student("赵六", 22, "女", 1.74);
        students[4] = new Student("孙七", 12, "男", 1.75);

        /*for (Student student : students) {
            System.out.println(student);
        }*/

        //将数组按照年龄升序排序
        /*Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });*/

        Arrays.sort(students, (Student o1, Student o2) -> {
            return o1.getAge() - o2.getAge();
        });

        //Lambda表达式的简化
        //1.参数类型可以全部省略不写
        Arrays.sort(students, (o1, o2) -> {
            return o1.getAge() - o2.getAge();
        });

        //2.如果Lambda表达式的参数列表只有一个参数，那么参数列表的括号可以省略不写
        Arrays.sort(students, (o1, o2) -> {
            return o1.getAge() - o2.getAge();
        });

        //3.如果Lambda表达式的代码块只有一行代码，那么大括号和分号可以省略不写；
        // 如果这行代码是return语句，那么return关键字可以省略不写
        Arrays.sort(students, (o1, o2) -> o1.getAge() - o2.getAge());

        for (Student student : students) {
            System.out.println(student);
        }
    }
}
